package pro.shuangxi.file;

import org.springframework.core.io.ClassPathResource;

import java.awt.*;
import java.io.InputStream;

/**
 * @author mengshx
 * @description <p>load font</p>
 * @date 2022-11-16 11:54
 **/
public class FontUtils {
    public static Font loadFont(String fontFile, float fontSize) {

        try {
            ClassPathResource resource = new ClassPathResource(fontFile);
            InputStream aixing = resource.getInputStream();
            Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing);
            Font dynamicFontPt = dynamicFont.deriveFont(fontSize);

            GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
            localGraphicsEnvironment.registerFont(dynamicFontPt);
            aixing.close();
            return dynamicFontPt;
        } catch (Exception e)//异常处理
        {
            e.printStackTrace();
            return new java.awt.Font("宋体", Font.PLAIN, (int) fontSize);
        }


    }
}
